

package images;

import Composite.Photo;
import Composite.Dossier;
import Composite.Element;
import fichiers.*;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.table.TableCellRenderer;

public class JTableDetail extends JTable implements MouseListener,KeyListener{
    
    DetailTableModel model;
    TabsDeDroite lesTabs;
    
    public JTableDetail(DetailTableModel model,TabsDeDroite lesTabs) {
        super(model);
        
        this.model = model;
        this.lesTabs = lesTabs;
        
        
        setFillsViewportHeight(true);
        setCellSelectionEnabled(true);
        //   monTableau.setRowHeight(xApercu);
        //   monTableau.setIntercellSpacing(new Dimension(10, 15));
        //   monTableau.setShowGrid(false);
        //  monTableau.getTableHeader().setReorderingAllowed(false);
        //  monTableau.getTableHeader().setResizingAllowed(false);
        
        setShowHorizontalLines(false);
        setShowVerticalLines(false);
        setDragEnabled(true);
        
        setAutoCreateRowSorter(true);
        
        addMouseListener(this);
        addKeyListener(this);
        
        
        
        
        setDefaultRenderer(JLabel.class, new JLabelTableCellRenderer());
        
        
        
        setColumnSelectionAllowed(false);
        setRowSelectionAllowed(true);
        for(int i=0;i < getColumnModel().getColumnCount(); i++) {
            
            getColumnModel().getColumn(i).setPreferredWidth(200);
            
        }
        
        //    monTableau.setTableHeader(null);
        setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);    // pour un seul : SINGLE_SELECTION
        //   monTableau.setAutoResizeMode(monTableau.AUTO_RESIZE_OFF);
        
        //   this.add(monScrollPane,BorderLayout.CENTER);
        
        
        
        
    }
    
    
    public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e)) {
            
            int index = convertRowIndexToModel(rowAtPoint(e.getPoint()));
            Element element =(Element)model.getElementAt(index);
            if(element instanceof Photo){
                int numeroRetour = lesTabs.getSelectedIndex();
                lesTabs.creerVueImage((Photo)model.getElementAt(index), index, numeroRetour);
            }else{
                model.setModelAdapte((Dossier)element);
            }
            
        }else if(SwingUtilities.isMiddleMouseButton(e)) { // middle click
            
            int index = convertRowIndexToModel(rowAtPoint(e.getPoint()));
            Element element =(Element)model.getElementAt(index);
            if(element instanceof Photo){  // on fait qquechose que quand une image
                int numeroRetour = lesTabs.getSelectedIndex();
                lesTabs.creerVueImageBackground((Photo)model.getElementAt(index),index, numeroRetour);
            }
        }
    }
    
    
    public void  keyPressed(KeyEvent e) {
        int touche = e.getKeyCode();
        if (touche  == KeyEvent.VK_ENTER) {
            int index[] = getSelectedRows();
            int premierPlan = 0;
            int numeroRetour = lesTabs.getSelectedIndex();
            
            for (int i = 0; i < index.length; i++) {
                Element element =(Element)model.getElementAt(index[i]);
                if(element instanceof Photo){
                    
                    
                    if(premierPlan == 0) {
                        lesTabs.creerVueImage((Photo)model.getElementAt(convertRowIndexToModel(index[i])), convertRowIndexToModel(index[i]), numeroRetour);
                        premierPlan = 1;
                    }else{
                        lesTabs.creerVueImageBackground((Photo)model.getElementAt(convertRowIndexToModel(index[i])),convertRowIndexToModel(index[i]), numeroRetour);
                        
                    }
                }else{ // c'est un dossier, on fait qquechose que si y'a qu'un dossier
                    if (index.length == 1){
                        model.setModelAdapte((Dossier)element);
                    }
                    
                }
            }
            
            
        }
    }
    
    public void keyReleased(KeyEvent e){}
    public void keyTyped(KeyEvent e){}
    
    
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e){}
    public void mouseMoved(MouseEvent e) {}
    public void mousePressed(MouseEvent e){}
    public void mouseReleased(MouseEvent e) {}
    
}



class JLabelTableCellRenderer implements TableCellRenderer{

    
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
        JLabel leJLabel = new JLabel();
        leJLabel.setOpaque(true);
        
        Element element =(Element)value;
        
        leJLabel.setText(value.toString());
        leJLabel.setToolTipText(value.toString());
        
            leJLabel.setIcon(element.getRepresentation());

        
        if(isSelected){
            leJLabel.setForeground(table.getBackground());
            leJLabel.setBackground(table.getForeground());
            
        }else{
            
            leJLabel.setForeground(table.getForeground());
            leJLabel.setBackground(table.getBackground());
            
        }
        
        return leJLabel;
    }
}




